/**
 * <p>Title: Asteroid</p>
 * <p>Description: The representation of an asteroid... flying rocks... space...
 * the game...?</p>
 * @author Guilherme Mauro Germoglio
 */

import javax.swing.*;
import java.awt.*;
import java.util.*;

public class Asteroid extends Body {

  //atributes
  public boolean wasHit;
  private int vertex;
  private double[] vertexRadiuses;
  private int radius;
  private LinkedList lastCollidedIndex;

  //methods
  /**
   * The constructor of an asteroid. The position is its parameter.
   * @param x The inicial X coordinate
   * @param y The inicial Y coordinate
   * @param x_velocity The inicial asteroid's speed in X axis
   * @param y_velocity The inicial asteroid's speed in Y axis
   * @param direction The angle between the trajetory and the horizontal axis
   * @param vertex The number of vertex of the asteroid
   * @param radius The medium radius of the asteroid
   */
  public Asteroid (int x, int y, double x_velocity, double y_velocity, int direction, int vertex, int radius) {
    super(x,y,x_velocity, y_velocity,direction);
    this.radius = radius;
    this.wasHit = false;
    this.vertex = vertex;
    this.vertexRadiuses = new double[vertex];
    setVertexRadiuses();
    this.lastCollidedIndex = new LinkedList();
  }

  /**
   * Returns the number of vertex of the asteroid
   * @return The number of vertex
   */
  public int getVertex() {
    return vertex;
  }

  public int getRadius() {
    return radius;
  }

  /**
   * Sets the condition that the asteroid was hit
   */
  public void hasBeenHit() {
    wasHit = true;
  }

  /**
   * Shows if the asteroid was hit or not
   * @return true if was hit, false case not
   */
  public boolean wasHit() {
    return wasHit;
  }

  /**
   * Sets the asteroid's various radiuses - in other words: fills vertexRadiuses
   */
  private void setVertexRadiuses() {
    for (int k = 0; k < vertex; k++) {
      vertexRadiuses[k] = (Math.random() * 20) + radius;
    }
  }

  /**
   * Draws and returns the asteroid
   * @param x The X coordinate
   * @param y The Y coordinate
   * @param direction The angle between the trajetory and the horizontal axis
   * @return A polygon which is an asteroid
   */
  public Polygon drawAsteroid(double x, double y, double direction) {
    Polygon p = new Polygon();
    for (int k = 0; k < getVertex(); k++) {
      p.addPoint(
      (int) (x + vertexRadiuses[k] * Math.cos(direction + k * 2 * Math.PI /getVertex() )),
      (int) (y + vertexRadiuses[k] * Math.sin(direction + k * 2 * Math.PI/ getVertex() ))
      );
    }
    return p;
  }

  /**
   * Draws and returns the asteroid
   * @return A polygon which is an asteroid
   */
  public Polygon drawAsteroid() {
	return drawAsteroid(super.getX(), super.getY(), super.getDirection());
  }

  /**
   * Sets the lastCollidedIndex, which is the index of the last asteroid that collided
   * with this one.
   * @param newIndex The new index
   */
  public synchronized void collidedWith(int newIndex) {
    lastCollidedIndex.addFirst(new Integer(newIndex));
    if (lastCollidedIndex.size() == 1) {
      (new Counter()).start(lastCollidedIndex);
    }
  }

  /**
   * Checks if the asteroid has just collided with the other asteroid specified
   * in the parameter.
   * @param otherAsteroidIndex The other asteroid's index
   * @return true if had already collided. false case not.
   */
  public boolean hasJustCollidedWith(int otherAsteroidIndex) {
    return lastCollidedIndex.contains(new Integer(otherAsteroidIndex));
  }
}

/**
 * <p>Title: Counter </p>
 * <p>Description: It's a like a regressive counter. Useful to make the asteroid
 * "forget" which asteroid it had just collided. </p>
 * @author Guilherme Mauro Germoglio
 */
class Counter
    extends Thread {

  private LinkedList lastCollidedIndex;
  /**
   * Will start the thread, receiving the list containg the indexes of the
   * last asteroids this asteroid collided with.
   * @param lastCollidedIndex The list containing the indexes
   */

  public void start(LinkedList lastCollidedIndex) {
    this.lastCollidedIndex = lastCollidedIndex;
    super.start();
  }

  /**
   * Runs the thread. Will wait a second. After this the first asteroid
   * that collided with this asteroid is "forgotten".
   */

  public void run() {
    while (lastCollidedIndex.size() != 0) {
      try {
        super.sleep(1000);
      }
      catch (InterruptedException e) {}
      lastCollidedIndex.removeLast();
    }
  }
}

